package com.go.where.manager.action.go;

import java.io.File;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;

import com.go.where.manager.action.SuperAction;
import com.go.where.manager.common.Constants;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.pojo.AlbumInfo;
import com.go.where.manager.service.AlbumService;
import com.go.where.manager.util.CommonUtil;
import com.go.where.manager.util.KindEditUtil;

@InterceptorRefs( {
		@InterceptorRef(value = "defaultStack", params = { "maximumSize",
				"2048000", "allowedTypes",
				"image/bmp,image/png,image/gif,image/jpeg" }),
		@InterceptorRef(value = "defalutStackSet") })
public class AlbumAction extends SuperAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4383195694100314090L;
	private static final Logger log = Logger.getLogger(AlbumAction.class);

	public Pagination<?> page;

	@Resource(name = "albumService")
	private AlbumService albumService;

	private AlbumInfo albumInfo;
	private AlbumInfo albumInfoQuery;

	private String id;

	private File albumCover;// 上传的文件
	private String albumCoverFileName; // 文件名称
	private String albumCoverContentType; // 文件类型

	/**
	 * 查询列表
	 * 
	 * @return
	 */
	public String list() {
		if (albumInfoQuery == null) {
			albumInfoQuery = new AlbumInfo();
			albumInfoQuery.setPageSize(Constants.INIT_PAGESIZE);
			albumInfoQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		try {
			albumInfoQuery.setIsDelete(Constants.NOT_DELETED);
			page = albumService.queryPage(albumInfoQuery);
		} catch (Exception e) {
			addActionError(e.getMessage());
			log.error("查询信息异常", e);
		}
		return LIST;
	}

	public String toEdit() {
		albumInfo = albumService.queryByPrimartKey(id);
		return INPUT;
	}

	public String add() {
		return INPUT;
	}

	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() {
		try {
			String coverRealpath = ServletActionContext.getServletContext()
					.getRealPath(
							CommonUtil.FILE_SEPARATOR
									+ Constants.ALBUM_COVER_IMAGES);
			String cover = KindEditUtil.uploadPicture(albumCover,
					albumCoverFileName, coverRealpath,
					Constants.ALBUM_COVER_IMAGES);
			albumInfo.setAlbumPath(cover);
			albumInfo.setMonth(albumInfo.getMonthStr());
			albumService.save(albumInfo, getCurrentOperator());
			redirectionUrl = "album!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("保存信息异常", e);
			addActionError("保存信息异常！" + e);
			return this.add();
		}
	}

	/**
	 * 编辑
	 * 
	 * @return
	 */
	public String edit() {
		try {
			if (albumCover != null) {
				String previewRealpath = ServletActionContext
						.getServletContext().getRealPath(
								CommonUtil.FILE_SEPARATOR
										+ Constants.ALBUM_COVER_IMAGES);
				String cover = KindEditUtil.uploadPicture(albumCover,
						albumCoverFileName, previewRealpath,
						Constants.ALBUM_COVER_IMAGES);
				albumInfo.setAlbumPath(cover);
			}
			albumInfo.setMonth(albumInfo.getMonthStr());
			albumService.update(albumInfo, getCurrentOperator());
			redirectionUrl = "album!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("修改信息异常", e);
			addActionError("修改信息异常！" + e);
			id = albumInfo.getId();
			return this.toEdit();
		}
	}

	/**
	 * 假删除
	 * 
	 * @return
	 */
	public String falseDelete() {
		albumInfo = new AlbumInfo();
		albumInfo.setId(id);
		albumInfo.setIsDelete(Constants.DELETED);
		try {
			albumService.update(albumInfo, getCurrentOperator());
			redirectionUrl = "album!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("删除信息异常", e);
			addActionError("删除信息异常" + e);
			return this.list();
		}
	}

	public Pagination<?> getPage() {
		return page;
	}

	public void setPage(Pagination<?> page) {
		this.page = page;
	}

	public AlbumInfo getAlbumInfo() {
		return albumInfo;
	}

	public void setAlbumInfo(AlbumInfo albumInfo) {
		this.albumInfo = albumInfo;
	}

	public AlbumInfo getAlbumInfoQuery() {
		return albumInfoQuery;
	}

	public void setAlbumInfoQuery(AlbumInfo albumInfoQuery) {
		this.albumInfoQuery = albumInfoQuery;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public File getAlbumCover() {
		return albumCover;
	}

	public void setAlbumCover(File albumCover) {
		this.albumCover = albumCover;
	}

	public String getAlbumCoverFileName() {
		return albumCoverFileName;
	}

	public void setAlbumCoverFileName(String albumCoverFileName) {
		this.albumCoverFileName = albumCoverFileName;
	}

	public String getAlbumCoverContentType() {
		return albumCoverContentType;
	}

	public void setAlbumCoverContentType(String albumCoverContentType) {
		this.albumCoverContentType = albumCoverContentType;
	}

}
